<?php
require_once 'models/TMSTypeAggregateModel.inc';

/**
 * 
 * @author kras
 */
class TMSTypeAttributesModel extends TMSTypeAggregateModel {
		
	/**
	 * returns name of list items class
	 *
	 * @return string
	 */
	public function ClassName(){
		return TMSTypeAttributeItem::GetClassName();
	}
	
	/**
	 * @return IPropertyMeta[]
	*/
	public function ClassProperties($object = null){
		return $this->TypesModel->AttributeProperties();
	}
	
	protected function getAggregates(TMSTypeItem $type){
		return $type->Attributes;
	}
	protected function getAggregate(TMSTypeItem $type,$nm){
		return new TMSTypeAttributeItem($type->Base, $nm, $type->AttrMeta[$nm], $this->TypesModel);
	}
	protected function getRawAggregates(TMSTypeItem $type){
		return $type->AttrMeta;
	}
	protected function setRawAggregates(TMSTypeItem $type,$meta){
		$type->AttrMeta = $meta;
	}
	protected function editData(TMSTypeItem $type){
		return array('Meta'=>$type->Meta);
	}
			
	/**
	 * @return IItem
	*/
	public function GetDummyItem($id = null, $class = null){
		return new TMSTypeAttributeItem($this->TypesModel->GetDummyItem()->Base, $id, (object)array(
					"Caption" => "",
					"Type" => "",
					"Size" => "",
					"IsKitOption"=>false,
					"Indexed" => false,
					"Selection" => "",
					"RefClass" => "",
					"BackReference"=>"",
					"Required" => false
				), $this->TypesModel);
	}
	
	protected function parseData($nm,$data){
		$attr = array(
				'Name' => $nm,
				'Caption' => isset($data['Caption'])?$data['Caption']:'',
				'Selection'=> isset($data['Selection'])?$data['Selection']:''
		);
		
		if (isset($data['Type']))
			$attr['Type'] = $data['Type'];
		if (isset($data['Size']))
			$attr['Size'] = $data['Size'];
		if (isset($data['Indexed']))
			$attr['Indexed'] = TConvertions::ConvertToBoolean($data['Indexed']);
		
		if (isset($data['IsKitOption']))
			$attr['IsKitOption'] = TConvertions::ConvertToBoolean($data['IsKitOption']);		
		
		if (isset($data['RefClass'])  && isset($data['Type'])){
			switch ($data['Type']){
				case TItemPropertyType::PT_REFERENCE:{
					$attr['RefClass'] = $data['RefClass'];
					$attr['Indexed'] = true;
				}break;
				case TItemPropertyType::PT_COLLECTION:
					if (isset($data['BackReference'])){
						$attr['RefClass'] = $data['RefClass'];
					}break;
				case TItemPropertyType::PT_SET:{
					$attr['RefClass'] = $data['RefClass'];
					$attr['BackReference'] = 'Master';
				}break;
			}
		}
		
		if (isset($data['BackReference']) && $data['BackReference'] && !isset($attr['BackReference']))
			$attr['BackReference'] = $data['BackReference'];
		
		if (isset($data['SortBy']) && $data['SortBy'])
			$attr['SortBy'] = $data['SortBy'];
		
		if (isset($data['Required']))
			$attr['Required'] = TConvertions::ConvertToBoolean($data['Required']);
		return (object)$attr;
	}	
	
	/**
	 * @param TListOptions $options
	 * @return int
	 */
	public function GetCount(TListOptions $options = null){return 0;}
}